package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {
    @Resource
    private CheckGroupDao checkGroupDao;
    @Resource
    private CheckItemDao checkItemDao;
    /**
     * 检查组分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询检查组信息,返回一个集合
        Page<CheckGroup> page = checkGroupDao.findPage(queryPageBean.getQueryString());

        PageResult pageResult = new PageResult();
        pageResult.setRows(page.getResult());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    /**
     * 新添检查组
     * @param checkGroup
     * @param ids
     */
    @Override
    @Transactional
    public void addCheckGroup(CheckGroup checkGroup,List<Integer> ids) {

        List<CheckItem> checkItems = new ArrayList<>();
        for (Integer id : ids) {
           checkItems.add(checkItemDao.selectById(id));
        }
        checkGroup.setCheckItems(checkItems);
        if(checkGroupDao.selectCodeOrName(checkGroup.getCode(),checkGroup.getName()) > 0){
            throw new BusinessRuntimeException(MessageConst.ADD_CHECKGROUP_FAIL);
        }
        checkGroupDao.addCheckGroup(checkGroup);
        checkGroupDao.addGroupAndItem(checkGroup.getCheckItems(),checkGroup.getId());

    }

    @Override
    public CheckGroup selectGroupById(Integer id) {
        return checkGroupDao.selectGroupById(id);
    }

    @Override
    public List<Integer> selectItemIdsByCheckGroupId(Integer id) {

        return checkGroupDao.selectItemIdsByCheckGroupId(id);
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param ids
     */
    @Override
    @Transactional
    public void updateGroup(CheckGroup checkGroup, List<Integer> ids) {
        //如果传入的ids集合与查询的到的item的id集合不一致
        if(!Objects.equals(ids,checkGroupDao.selectItemIdsByCheckGroupId(checkGroup.getId()))){
            List<CheckItem> checkItems = new ArrayList<>();
            for (Integer id : ids) {
                checkItems.add(checkItemDao.selectById(id));
            }
            checkGroupDao.deleteGroupAndItem(checkGroup.getId());
            checkGroupDao.addGroupAndItem(checkItems,checkGroup.getId());
        }
        checkGroupDao.updateGroup(checkGroup);
    }

    /**
     * 删除检查组
     * @param id
     */
    @Override
    public void deleteCheckGroupitemById(Integer id) {
        //查询与此检查组关联的套餐信息集合
        List<Setmeal>list = checkGroupDao.selectSetmealRelevanceGroup(id);
        //如果集合内有数据,抛异常,"该检查组已关联套餐,不可删除"
        if(!list.isEmpty()){
            throw new BusinessRuntimeException("该检查组已关联套餐,不可删除");
        }
        //删除关联表数据
        checkGroupDao.deleteGroupAndItem(id);
        //没集合数据就删除
        checkGroupDao.deleteGroup(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> list = checkGroupDao.findAll();
        return list;
    }
}
